package com.example.flshtarvel.service;

import com.example.flshtarvel.entity.*;
import com.example.flshtarvel.mapper.ArticleMapper;
import com.example.flshtarvel.mapper.ImageMapper;
import com.example.flshtarvel.mapper.TagArticleMapMapper;
import com.example.flshtarvel.mapper.TagMapper;
import com.example.flshtarvel.util.RemoveDuplicate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
public class ArticleService {

    @Autowired(required = false)
    ArticleMapper articleMapper;
    @Autowired(required = false)
    ImageMapper imageMapper;
    @Autowired(required = false)
    TagMapper tagMapper;
    @Autowired(required = false)
    TagArticleMapMapper tagArticleMapMapper;

    public int insertArticle(Article article){
        if(articleMapper.getArticeByName(article.getArticleTypeId(),article.getArticleName())==null){
            article.setArticleId(UUID.randomUUID().toString());
            return articleMapper.insertArticle(article);
        }else{
            return 2;
        }

    }

    public Article selectArticleById(String articleId){
        Article article = articleMapper.selectArticleById(articleId);
        List<Image> imageList = imageMapper.selectImageByArticleId(articleId);
        article.setImageList(imageList);
        return article;
    }

    public List<Article> selectAllArticleByArticleTypeName(String articleTypeName,int page,int size){
        int offset = (page-1)*size;
        return articleMapper.selectAllArticleByArticleTypeName(articleTypeName,offset,size);
    }

    public List<Article> selectAllArticleByArticleTypeId(String articleTypeId,int page,int size){
        int offset = (page-1)*size;
        List<Article> articleList = articleMapper.selectAllArticleByArticleTypeId(articleTypeId,offset,size);
        //查出图片
        for(Article article : articleList){
            List<Image> imageList = imageMapper.selectImageByArticleId(article.getArticleId());
            article.setImageList(imageList);
        }

        return articleList;
    }

    public List<Article> selectArticleByName(String articleTypeId,String articleName){
        String likeSql = "%" + articleName +"%";
        return articleMapper.selectArticleByName(articleTypeId,likeSql);
    }

    public int selectArticleCountByArticleTypeName(String articleTypeName){
        return articleMapper.selectArticleCountByArticleTypeName(articleTypeName);
    }

    public int selectArticleCountByArticleTypeId(String articleTypeId){
        return articleMapper.selectArticleCountByArticleTypeId(articleTypeId);
    }

    public int updateArticle(Article article){
        return articleMapper.updateArticle(article);
    }

    public int deleteArticle(String articleId){
        return articleMapper.deleteArticle(articleId);
    }

    //不分类查询文章，通过文章名称模糊查询
    public List<Article> selectAllArticleByName(String likeSql){
        List<Article> articleList = new ArrayList<Article>();
        likeSql = "%" + likeSql + "%";
        articleList = articleMapper.selectAllArticleByName(likeSql);
        return articleList;
    }

    //首页搜索文章
    public List<Article> searchArticle(String search){
        search = "%" + search + "%";
        List<Article> articleList = new ArrayList<Article>();
        //根据文章名称查
        articleList = articleMapper.selectAllArticleByName(search);
        //根据标签名称查
        List<Tag> tagList = tagMapper.selectTagByName(search);
        //遍历tagLst，通过标签id查Article（tag_article_map表）
        List<TagArticleMap> tagArticleMapList = new ArrayList<TagArticleMap>();
        for(Tag tag : tagList){
            tagArticleMapList.addAll(tagArticleMapMapper.selectArticleByTagId(tag.getTagId()));
        }
        //遍历tagArticleMapList,通过articleId查article（article表）
        for(TagArticleMap tagArticleMap : tagArticleMapList){
            articleList.add(articleMapper.selectArticleById(tagArticleMap.getArticleId()));
        }
        //去重
        RemoveDuplicate removeDuplicate = new RemoveDuplicate();
        articleList = removeDuplicate.removeDuplicate_2((ArrayList) articleList);


        //查出图片
        for(Article article : articleList){
           List<Image> imageList = imageMapper.selectImageByArticleId(article.getArticleId());
           article.setImageList(imageList);
        }

        return articleList;
    }
}
